﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for WarehouseListBZ
/// </summary>
public class WarehouseListBZ : InventoryListBZ
{
	public WarehouseListBZ()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    override public string LabelListName
    {
        get { return "入库单管理"; }
    }

    override public string LabelDate
    {
        get { return "入库日期"; }
    }

    override public string LabelListNumber
    {
        get { return "入库单号"; }
    }

    override public string ButtonSave
    {
        get { return "保存入库单"; }
    }

    override public string ButtonClose
    {
        get { return "关闭入库单"; }
    }

    override public void GetListData(int listid, out DataTable dt, out int warehouseid, out DateTime date, out string listnumber)
    {
        DataTable tbl = new DataTable();
        
        using (SqlConnection connect = new SqlConnection(PublicDefine.SQLConnectString))
        {
            connect.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = connect;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select warehouseid,createddate,listnumber from tbl_WarehouseList where WarehouseListID=" + listid.ToString();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            adapter.Fill(tbl);
            if (0 == tbl.Rows.Count)
            {
                throw new Exception("入库单"+listid.ToString() +"不存在！");
            }
            warehouseid = (int)tbl.Rows[0]["WarehouseID"];
            date = (DateTime)tbl.Rows[0]["CreatedDate"];
            listnumber = tbl.Rows[0]["ListNumber"].ToString();
            tbl = new DataTable();
            cmd.CommandText = "Select ProductID,Quantity from tbl_WarehouseListDetail where WarehouseListID =" + listid.ToString();
            adapter.Fill(tbl);
            if (0 == tbl.Rows.Count)
            {
                throw new Exception("入库单" + listid.ToString() + "没有明细！");
            }
            // get the details 
            dt = GetEmptyList();
            string code, name, module, color;
            int id;
            decimal quantity;
            for (int i = 0; i < tbl.Rows.Count; i++)
            {
                id = (int)tbl.Rows[i]["ProductID"];
                quantity = (decimal)tbl.Rows[i]["Quantity"];
                GetProductInfo(id,out code,out name,out module,out color);
                DataRow dr = dt.NewRow();
                dr["ProductID"] = id; 
                dr["ProductCode"] = code;
                dr["ProductName"] = name;
                dr["ProductModule"] = module;
                dr["ProductColor"] = color;
                dr["Quantity"] = quantity;
                dt.Rows.Add(dr);
            }
            connect.Close();
        }
    }

    override public int AddList(SqlCommand cmd, int warehouseid, DateTime createddate, string listnumber, DataTable dt)
    {
        SqlParameter parameter;
        int warehouselistid = 0;
        int productid;
        decimal quantity;
        int warehouselistdetailid = 0;

        //1 insert into tbl_warehouselist
        cmd.CommandText = "sp_WarehouseList_Add";
        cmd.Parameters.Clear();
        parameter = new SqlParameter("@WarehouseID", warehouseid);
        cmd.Parameters.Add(parameter);
        parameter = new SqlParameter("@CreatedDate", createddate);
        cmd.Parameters.Add(parameter);
        parameter = new SqlParameter("@ListNumber", listnumber);
        cmd.Parameters.Add(parameter);
        parameter = new SqlParameter("@WarehouseListID", warehouselistid);
        parameter.Direction = ParameterDirection.InputOutput;
        cmd.Parameters.Add(parameter);
        cmd.ExecuteNonQuery();
        warehouselistid = (int)cmd.Parameters["@WarehouseListID"].Value;
        //insert the detail into tbl_warehouselistdetail
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = dt.Rows[i];
            productid = (int)dr["ProductID"];
            quantity = (decimal)dr["Quantity"];
            cmd.CommandText = "sp_WarehouseListDetail_Add";
            cmd.Parameters.Clear();
            parameter = new SqlParameter("@WarehouseListID", warehouselistid);
            cmd.Parameters.Add(parameter);
            parameter = new SqlParameter("@ProductID", productid);
            cmd.Parameters.Add(parameter);
            parameter = new SqlParameter("@Quantity", quantity);
            cmd.Parameters.Add(parameter);
            parameter = new SqlParameter("@WarehouseListDetailID", warehouselistdetailid);
            parameter.Direction = ParameterDirection.InputOutput;
            cmd.Parameters.Add(parameter);
            cmd.ExecuteNonQuery();
            warehouselistdetailid = (int)cmd.Parameters["@WarehouseListDetailID"].Value;
        }
        return warehouselistid;
    }

    override public void DeleteList(SqlCommand cmd, int ListID)
    {
        SqlParameter parameter;
        cmd.CommandText = "sp_WarehouseList_Delete";
        cmd.Parameters.Clear();
        parameter = new SqlParameter("@WarehouseListID", ListID);
        cmd.Parameters.Add(parameter);
        cmd.ExecuteNonQuery();
    }
}
